﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Bitirme.DAL.Entities
{
    public class ScheduledWorkPackages:WorkPackage
    {
        private List<Employee> _ProperEmployees;
        private bool _isAlreadyAssigned;
        private bool _isScheduled;
        private List<Assignment> _Assignment;
        private DateTime _InitialStartDate;
        private DateTime _InitialEndDate;
        private List<ScheduledWorkPackages> _ScheduledDepedency;

        public bool isAlreadyAssigned
        {
            get { return _isAlreadyAssigned; }
            set { _isAlreadyAssigned = value; }
        }

        public bool isScheduled
        {
            get { return _isScheduled; }
            set { _isScheduled = value; }
        }

        public List<Assignment> Assignment
        {
            get { return _Assignment; }
            set { _Assignment = value; }
        }

        public List<ScheduledWorkPackages> ScheduledDepedency
        {
            get { return _ScheduledDepedency; }
            set { _ScheduledDepedency = value; }
        }

        public List<Employee> ProperEmployees
        {
            get { return _ProperEmployees; }
            set { _ProperEmployees = value; }
        }

        public DateTime InitialStartDate
        {
            get { return _InitialStartDate; }
            set { _InitialStartDate = value; }
        }
        public DateTime InitialEndDate
        {
            get { return _InitialEndDate; }
            set { _InitialEndDate = value; }
        }

        public ScheduledWorkPackages(WorkPackage package)
        {
            this.ID = package.ID;
            this.Name = package.Name;
            this.Requirement = package.Requirement;
            this.Subject = package.Subject;
            this.PackageState = package.PackageState;
            this.Dependency = package.Dependency;
            this._ProperEmployees = new List<Employee>();
            this._isAlreadyAssigned = false;
            this._isScheduled = false;
            this._Assignment = new List<Assignment>();
            this._InitialStartDate = new DateTime();
            this._InitialEndDate = new DateTime();
            this._ScheduledDepedency = new List<ScheduledWorkPackages>();

        }

        /*** Assignmentlar startDate'e göre sorted geliyor..***/
        public bool arrangeAssignments() {
            bool result = true;
            float workHour = 0;
            float remainedWorkHour = 0;
            float totalHourPerDay = 0;
            int count = 0;
            if (_Assignment.Count > 1)
            {
                for (int i = 0; i < _Assignment.Count - 1; i++)
                {

                    if (DateTime.Compare(_Assignment[i + 1].StartDate, _Assignment[i].StartDate) > 0)
                        totalHourPerDay += _Assignment[i].HourPerDay;
                    else
                        totalHourPerDay += _Assignment[i + 1].HourPerDay;

                    //totalHourPerDay += _Assignment[i].HourPerDay;
                   // workHour += _Assignment[i].HourPerDay * Math.Abs((_Assignment[i + 1].StartDate.Day - _Assignment[i].StartDate.Day));

                    // Farklı ayda günlerin farkını hesaplamak
                    while(DateTime.Compare(_Assignment[i + 1].StartDate, _Assignment[i].StartDate) != 0){
                        count++;
                        if (DateTime.Compare(_Assignment[i + 1].StartDate, _Assignment[i].StartDate) > 0) {
                            if (DateTime.Compare(_Assignment[i + 1].StartDate, _Assignment[i].StartDate.AddDays(count)) == 0)
                            {break;}
                        }
                        else if (DateTime.Compare(_Assignment[i + 1].StartDate, _Assignment[i].StartDate) < 0)
                        {
                            if (DateTime.Compare(_Assignment[i+1].StartDate.AddDays(count), _Assignment[i].StartDate) == 0)
                            { break; }
                        }
                    }
                    //===============================================================

                    if (DateTime.Compare(_Assignment[i + 1].StartDate, _Assignment[i].StartDate) > 0)
                    {
                        workHour += count * _Assignment[i].HourPerDay;
                        totalHourPerDay += _Assignment[i + 1].HourPerDay;
                    }
                    else
                    {
                        workHour += count * _Assignment[i + 1].HourPerDay;
                        totalHourPerDay += _Assignment[i].HourPerDay;
                    }
                    //totalHourPerDay += _Assignment[i + 1].HourPerDay; // hangisi sonra bilmiyoruz degismesi lazim

                    remainedWorkHour = Requirement.ManHour - workHour;
                    //if (_Assignment[i].StartDate.Day == 1)
                    if(remainedWorkHour>0.0)
                        Assignment[i + 1].EndDate = _Assignment[i + 1].StartDate.AddDays(Math.Ceiling(remainedWorkHour / totalHourPerDay) - 1);
                    //else
                        //Assignment[i + 1].EndDate = _Assignment[i + 1].StartDate.AddDays(Math.Ceiling(remainedWorkHour / totalHourPerDay));

                    if (DateTime.Compare(_Assignment[i].EndDate, _Assignment[i + 1].EndDate) > 0)
                    {
                        _Assignment[i].EndDate = _Assignment[i + 1].EndDate;
                        _InitialEndDate = _Assignment[i + 1].EndDate;
                    }
                    else{
                        _Assignment[i + 1].EndDate = _Assignment[i].EndDate;
                        _InitialEndDate = _Assignment[i].EndDate;
                    }
                }
            }
            else {
                _InitialEndDate = _Assignment[0].EndDate;
            }
            _InitialStartDate = _Assignment[0].StartDate; //Assignmentlar startDate'e göre sorted geldiği için en erkeni initial'a atıyoruz

            for (int i = 0; i < _Assignment.Count; i++)
            {
                if (DateTime.Compare(_Assignment[i].EndDate, _Assignment[i].StartDate) <= 0)
                {
                    _Assignment.Remove(_Assignment[i]);
                    result = false;
                }
            }

            return result;
        }
    }
}